<?php
/**
 * RUBIK IT Solution & Development Co.ltd,
 *
 * PROJECT		: rTech
 * COPYRIGHT	: 2008
 * ------------------------------------------------------
 *
 * DTO class of  #__user table
 *
 * @author			generator <hoadn@rubik.com.vn>
 * @access			public
 * @package			dbo.UserDTO
 *
 * Created on	: Wednesday, October 14, 2009
 */

// Importer
RCoreImporter::cls( "dbo.AbstractDTO" );
RClassesImporter::cls('dbo.dtos.AuctionDTO');
RClassesImporter::cls('dbo.dtos.VehicleDTO');

/**
 * @name ExcelAuctionObject
 *
 *
 */
class ExcelAuctionObject extends Object {

	var $owner_id;
	var $row_number;
	
	// Keys
	var $item_num;

	var $start_date;
	
	var $sale_date;
	var $sale_time;
	
	var $time_zone;
	var $lot_num;
	
	var $cate;
	var $cate_id;
	
	var $year;
	
	var $maker;
	var $maker_id;
	
	var $model_id;
	var $model;
	var $model_detail;
	
	var $body_style;
	var $color;
	
	var $damage_id;
	var $damage;
	
	var $secondary_damage_id;
	var $secondary_damage;
	
	var $sale_title_state;

	var $sale_title_type_id;
	var $sale_title_type;
	
	var $haskey;
	var $vin;
	var $odometer;
	var $odometer_brand;
	var $lot_acv;
	var $repair_cost;

	var $engine;
	
	var $drive_id;
	var $drive;
	
	var $transmission;

	var $fuel;
	var $fuel_id;
	
	var $cylinder;
	var $runs_drives;
	var $special_note;
	var $currency_code;
	var $images;
	var $video;
	var $end_date;
	
	/**
	 * Update all missed field
	 */
	function updateAllMissedFields() {
		$this->updateId('cate', 'CategoryDTO');
		$this->updateId('maker', 'MakerDTO');
		$this->updateId('model_group', 'ModelDTO');
		$this->updateId('damage', 'DamageDTO');
		$this->updateId('secondary_damage', 'DamageDTO');
		$this->updateId('drive', 'DriveDTO');
		$this->updateId('fuel', 'FuelDTO');
		$this->updateId('sale_title_type', 'SaletitletypeDTO');

		// process for date
		$this->start_date = $this->fixDateString($this->sale_date)." ".$this->fixTimeString($this->sale_time);
		$this->end_date = $this->fixDateString($this->end_date);
		
		
		// haskey
		if (strtoupper($this->haskey) == 'YES') {
			$this->haskey = true;
		} else {
			$this->haskey = false;
		}
		
		// video
		if ($this->video && strpos($this->video, 'http') === false) {
			//TODO
		}

		// images
		if ($this->images) {
			$images = explode(',', $this->images);
			$arrImages = array();
			RClassesImporter::cls("dbo.dtos.PhotoDTO");
			foreach ($images as $image) {
				$image = trim($image);
				if (!$image) {
					continue;
				}
				
				if (strpos($image, 'http') === false) {
					//update the image
					$image = trim($image);
				}
				$imageId = PhotoDTO::getId($image);
				if ($imageId) {
					$arrImages[] = $imageId;
				}
			}
			$this->images = $arrImages;
		}
	}

	/**
	 * Enter description here ...
	 * @param unknown_type $date
	 * @return string
	 */
	private function fixDateString($date) {
		if (!$date) {
			return null;
		}
		$year = substr($date, strlen($date)-4);
		$month = substr($date, strlen($date)-6, 2);
		$date = str_replace("{$month}{$year}", '', $date);
		return "{$year}-{$month}-{$date}";
	}

	/**
	 * Enter description here ...
	 * @param unknown_type $date
	 * @return string
	 */
	private function fixTimeString($time) {
		if (!$time) {
			return null;
		}
		$m = substr($time, strlen($time)-2);
		$h = substr($time, 0, strlen($time)-2);
		return "{$h}:{$m}:00";
	}
	
	private function isEmpty() {
		$properties = $this->getProperties();
		$count = count($properties);
		
		foreach ($properties as $key=>$pro) {
			if (empty($pro)) {
				$empty++;
			}
			
			if ($empty > $count*0.6) {
				return true;
			}
		}
		return false;
	}

	private function updateId($name, $dtoName) {
		$nameField = "{$name}";
		$idField = "{$name}_id";
		if (!$this->$idField && $this->$nameField) {
			RClassesImporter::cls("dbo.dtos.{$dtoName}");
			eval ('$this->'.$idField.' = '.$dtoName.'::getId($this->'.$nameField.');');
		}
	}

	function saveIntoDb() {
		if($this->isEmpty()) {
			return false;
		}
		$dto = new VehicleDTO();
		$dto->bind($this);
		SysDatabase::saveDTO($dto);
		
		if ($dto->hasError()) {
			$this->setError($dto->getAllErrorsMsg());
			return false;
		}
		
		$auctionDto = new AuctionDTO();
		$auctionDto->bind($this);
		$auctionDto->vehicle_id = $dto->vehicle_id;
		SysDatabase::saveDTO($auctionDto);
		
		if ($auctionDto->hasError()) {
			$this->setError($auctionDto->getAllErrorsMsg());
			return false;
		}
		
		// process images
		foreach ($this->images as $imgId) {
			$sql = "INSERT INTO vehicle_images (vehicle_id, image_id) VALUES ('{$dto->id}', '$imgId')";
			SysDatabase::execute($sql);
		}
		
		return true;
	}
}
?>